package com.shem.watermark.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.water.camera.R;


/**
 * 文字内容有描边的TextView
 * Author: zhangmiao
 * Date: 2018/4/13
 */
public class StrokeTextView extends AppCompatTextView {

    private TextView outlineTextView = null;
    private int mViewWidth = 0;
    private TextPaint mPaint;
    private Shader shader;
    private int[] color;
    private int angle;

    public StrokeTextView(Context context) {
        this(context, null);
    }

    public StrokeTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StrokeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        outlineTextView = new TextView(context, attrs, defStyleAttr);
        init(attrs);
        shader = getPaint().getShader();
    }

    @Override
    public void setTextSize(int unit, float size) {
        super.setTextSize(unit, size);
        outlineTextView.setTextSize(size);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mViewWidth == 0) {
            mViewWidth = getMeasuredWidth();
            if (mViewWidth > 0) {
                mPaint = getPaint();
            }
        }
    }

    private void init(AttributeSet attrs) {
        //1.获取参数
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.StrokeTextView);
        int stroke_color = ta.getColor(R.styleable.StrokeTextView_stroke_color, Color.WHITE);
        float stroke_width = ta.getDimension(R.styleable.StrokeTextView_stroke_width, 2);
        int start = ta.getColor(R.styleable.StrokeTextView_start_color, 0xffF9D0AB);
        int midColor = ta.getColor(R.styleable.StrokeTextView_mid_color, 0xffB0713C);
        int endColor = ta.getColor(R.styleable.StrokeTextView_end_color, 0xffF9D0AB);
        color = new int[]{start, midColor, endColor};
        angle = ta.getInt(R.styleable.StrokeTextView_angle, -2);
        ta.recycle();
        //2.初始化TextPaint
        TextPaint paint = outlineTextView.getPaint();
        paint.setStrokeWidth(stroke_width);
        paint.setStyle(Paint.Style.STROKE);
        outlineTextView.setTextColor(stroke_color);
        outlineTextView.setGravity(getGravity());
        outlineTextView.setTextSize(getTextSize());
    }

    @Override
    public void setLayoutParams(ViewGroup.LayoutParams params) {
        super.setLayoutParams(params);
        outlineTextView.setLayoutParams(params);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //设置轮廓文字
        CharSequence outlineText = outlineTextView.getText();

        if (outlineText == null || !outlineText.equals(getText())) {
            outlineTextView.setText(getText());
            postInvalidate();
        }
        outlineTextView.measure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        outlineTextView.layout(left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (outlineTextView != null) {
            if (outlineTextView.getTextSize() != getTextSize()) {
                outlineTextView.setTextSize(getTextSize());
            }
            outlineTextView.draw(canvas);
        }
        super.onDraw(canvas);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        if (outlineTextView != null) {
            outlineTextView.setText(text);
        }
        super.setText(text, type);
    }
}
